package com.gaoyi.crm.control;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.content.res.ResourcesCompat;

import com.gaoyi.crm.R;
import com.gaoyi.crm.util.DensityUtil;

public class TitleTextView extends View {

    private String mKey;
    private String mValue;
    private float mKeySize, mValueSize, mKeyBaseline, mTitleWidth, mTitleTextWidth;
    private int mKeyColor, mValueColor;
    private Paint mKetPaint;
    private TextPaint mValuePaint;
    private int mWidth, mHeight;
    private StaticLayout mStaticLayout;


    public TitleTextView(Context context) {
        super(context);
        init(context, null);
    }

    public TitleTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public TitleTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TitleTextView);
        mKey = ta.getString(R.styleable.TitleTextView_title);
        mKeySize = ta.getDimension(R.styleable.TitleTextView_title_textSize, DensityUtil.getSPValue(context, 18));
        mValueSize = ta.getDimension(R.styleable.TitleTextView_title_value_textSize, DensityUtil.getSPValue(context, 12));
        mTitleWidth = ta.getDimension(R.styleable.TitleTextView_title_width, DensityUtil.getDPValue(context, 120));
        mKeyColor = ta.getColor(R.styleable.TitleTextView_title_color, Color.BLACK);
        mValueColor = ta.getColor(R.styleable.TitleTextView_title_value_color, ResourcesCompat.getColor(getResources(), R.color.color_333, null));
        ta.recycle();
        mKetPaint = new Paint();
        mKetPaint.setColor(mKeyColor);
        mKetPaint.setTextSize(mKeySize);
        mValuePaint = new TextPaint();
        mValuePaint.setColor(mValueColor);
        mValuePaint.setTextSize(mValueSize);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mKey == null) return;
        mKey += ":";
        Paint.FontMetrics fm = mKetPaint.getFontMetrics();
        mTitleTextWidth = mKetPaint.measureText(mKey);
        mKeyBaseline = (mHeight - (fm.descent - fm.ascent)) / 2 - fm.ascent;// 左边居中显示
        canvas.drawText(mKey, mTitleWidth - mTitleTextWidth, mKeyBaseline, mKetPaint);
        if (mValue == null) return;
        canvas.save();
        mStaticLayout.draw(canvas);
        canvas.restore();
    }

    public void setValue(String value) {
        this.mValue = value;
        handleValue();
    }

    private void handleValue() {
        int mValueWidth = (int) mValuePaint.measureText(mValue);
        StaticLayout.Builder slb;
        int startWidth = (int) (mWidth - mTitleWidth - DensityUtil.dp2px(10));
        if (startWidth > mValueWidth) {
            slb = StaticLayout.Builder.obtain(mValue, 0, mValue.length(), mValuePaint, mValueWidth);
        } else {
            slb = StaticLayout.Builder.obtain(mValue, 0, mValue.length(), mValuePaint, startWidth);
        }
        mStaticLayout = slb.build();
        invalidate();
    }
}
